package cnsatmsmart.automation.mmi.view.dialogs;

import cnsatmsmart.CNSATMSMARTApp;
import cnsatmsmart.CNSATMSMARTView;
import cnsatmsmart.automation.mmi.helper.utilities.GlobalEnvVars;
import cnsatmsmart.automation.mmi.model.data.component.JTextFieldFilter;
import java.awt.Image;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author SIK - PTIK, BPPT
 */
public class radarSettings extends javax.swing.JDialog {

    /** Constructor - user defined constructor, no default constructor
     * membuat form radarSettings yang baru
     * @param parent jendela yang memanggilnya
     * @param modal ada atau tidaknya dialog
     */
        public radarSettings(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();

            Image frameIcon = GlobalEnvVars.kit.getImage(getClass().getResource("/cnsatmsmart/resources/"+GlobalEnvVars.icon));
            setIconImage(frameIcon);

            setTitle("Radar Location");

            txtLatDeg.setDocument(new JTextFieldFilter(JTextFieldFilter.NUMERIC));
            txtLatMin.setDocument(new JTextFieldFilter(JTextFieldFilter.NUMERIC));
            txtLatSec.setDocument(new JTextFieldFilter(JTextFieldFilter.FLOAT));
            txtLonDeg.setDocument(new JTextFieldFilter(JTextFieldFilter.NUMERIC));
            txtLonMin.setDocument(new JTextFieldFilter(JTextFieldFilter.NUMERIC));
            txtLonSec.setDocument(new JTextFieldFilter(JTextFieldFilter.FLOAT));

            loadDefaultProperty(); //menampilkan keberadaaan radar saat ini

            this.setResizable(false);
        }

    /* method - method lain yang dimiliki oleh kelas radarSetting */
        private void loadDefaultProperty()
        {
            String[] value = GlobalEnvVars.latRadar;
            if ((value == null) || (value.length != 4))
            {
                value = new String[]{"0","0","0.0","S"};
            }

            txtLatDeg.setText(value[0]);
            txtLatMin.setText(value[1]);
            txtLatSec.setText(value[2]);
            spinLatitude.setSelectedItem(value[3]);

            value = GlobalEnvVars.lonRadar;
            if ((value == null) || (value.length != 4))
            {
                value = new String[]{"0","0","0.0","E"};
            }

            txtLonDeg.setText(value[0]);
            txtLonMin.setText(value[1]);
            txtLonSec.setText(value[2]);
            System.out.println(value[2]);
            spinLongitude.setSelectedItem(value[3]);
        }

        private void savePropertyAsDefault()
        {
            String[] value = new String[4];
            int deg = Integer.parseInt(txtLatDeg.getText());
            int min = Integer.parseInt(txtLatMin.getText());
            float sec = Float.parseFloat(txtLatSec.getText());

            value[0] = String.valueOf(deg);
            value[1] = String.valueOf(min);
            value[2] = String.valueOf(sec);
            value[3] = spinLatitude.getSelectedItem().toString();
            GlobalEnvVars.latRadar = value;

            value = new String[4];
            deg = Integer.parseInt(txtLonDeg.getText());
            min = Integer.parseInt(txtLonMin.getText());
            sec = Float.parseFloat(txtLonSec.getText());

            value[0] = String.valueOf(deg);
            value[1] = String.valueOf(min);
            value[2] = String.valueOf(sec);
            value[3] = spinLongitude.getSelectedItem().toString();
            GlobalEnvVars.lonRadar = value;
            System.out.println(value[2]);
        }

        private Vector<String> validateInput()
        {
            Vector<String> listError = new Vector<String>();

            //validate Latitude
                String strDeg = txtLatDeg.getText();
                String strMin = txtLatMin.getText();
                String strSec = txtLatSec.getText();

                if (strDeg.isEmpty()) {
                    listError.add("Latitude degree field is empty");
                }
                if (strMin.isEmpty()) {
                    listError.add("Latitude minute field is empty");
                }
                if (strSec.isEmpty()) {
                    listError.add("Latitude second field is empty");
                }

                if (!strDeg.isEmpty() && !strMin.isEmpty() && !strDeg.isEmpty())
                {
                    int latDeg = Integer.parseInt(strDeg);
                    int latMin = Integer.parseInt(strMin);
                    Float latSec = Float.parseFloat(strSec);

                    if (latDeg > 90)
                        listError.add("Latitude degree value must be between 0-90");
                    if (latMin > 59)
                        listError.add("Latitude minute value must be between 0-59");
                    if (latSec > 3600.0f)
                        listError.add("Latitude second value must be between less than 3600");

                    float radarLatitude = (latDeg + (latMin / 60.0f) + (latSec / 3600.0f));
                    if (radarLatitude > 90.0f)
                    {
                        listError.add("Latitude value must be between 0-90");
                    }
                }

                //validate Longitude
                strDeg = txtLonDeg.getText();
                strMin = txtLonMin.getText();
                strSec = txtLonSec.getText();

                if (strDeg.isEmpty()) {
                    listError.add("Longitude degree field is empty");
                }
                if (strMin.isEmpty()) {
                    listError.add("Longitude minute field is empty");
                }
                if (strSec.isEmpty()) {
                    listError.add("Longitude second field is empty");
                }

                if (!strDeg.isEmpty() && !strMin.isEmpty() && !strDeg.isEmpty())
                {
                    int lonDeg = Integer.parseInt(strDeg);
                    int lonMin = Integer.parseInt(strMin);
                    float lonSec = Float.parseFloat(strSec);

                    if (lonDeg > 180)
                        listError.add("Latitude degree value must be between 0-180");
                    if (lonMin > 59)
                        listError.add("Latitude minute value must be between 0-59");
                    if (lonSec > 3600.0f)
                        listError.add("Latitude second value must be between less than 3600");

                    float radarLongitude = (lonDeg + (lonMin / 60.0f) + (lonSec / 3600.0f));
                    if (radarLongitude > 180.0f)
                    {
                        listError.add("Longitude value must be between 0-180");
                    }
                }

            return listError;
        }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtLatDeg = new javax.swing.JTextField();
        txtLatMin = new javax.swing.JTextField();
        txtLatSec = new javax.swing.JTextField();
        txtLonSec = new javax.swing.JTextField();
        txtLonMin = new javax.swing.JTextField();
        txtLonDeg = new javax.swing.JTextField();
        javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel6 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel7 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel4 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel5 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel8 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel9 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel10 = new javax.swing.JLabel();
        btnOK = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        spinLatitude = new javax.swing.JComboBox();
        spinLongitude = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cnsatmsmart.CNSATMSMARTApp.class).getContext().getResourceMap(radarSettings.class);
        txtLatDeg.setText(resourceMap.getString("txtLatDeg.text")); // NOI18N
        txtLatDeg.setName("txtLatDeg"); // NOI18N

        txtLatMin.setText(resourceMap.getString("txtLatMin.text")); // NOI18N
        txtLatMin.setName("txtLatMin"); // NOI18N

        txtLatSec.setText(resourceMap.getString("txtLatSec.text")); // NOI18N
        txtLatSec.setName("txtLatSec"); // NOI18N

        txtLonSec.setText(resourceMap.getString("txtLonSec.text")); // NOI18N
        txtLonSec.setName("txtLonSec"); // NOI18N

        txtLonMin.setText(resourceMap.getString("txtLonMin.text")); // NOI18N
        txtLonMin.setName("txtLonMin"); // NOI18N

        txtLonDeg.setText(resourceMap.getString("txtLonDeg.text")); // NOI18N
        txtLonDeg.setName("txtLonDeg"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cnsatmsmart.CNSATMSMARTApp.class).getContext().getActionMap(radarSettings.class, this);
        btnOK.setAction(actionMap.get("clickOK")); // NOI18N
        btnOK.setText(resourceMap.getString("btnOK.text")); // NOI18N
        btnOK.setName("btnOK"); // NOI18N

        btnCancel.setAction(actionMap.get("clickCancel")); // NOI18N
        btnCancel.setText(resourceMap.getString("btnCancel.text")); // NOI18N
        btnCancel.setName("btnCancel"); // NOI18N

        spinLatitude.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "N", "S" }));
        spinLatitude.setName("spinLatitude"); // NOI18N

        spinLongitude.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "W", "E" }));
        spinLongitude.setName("spinLongitude"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtLatDeg, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtLonDeg, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtLatMin, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtLonMin, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtLatSec, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel10)
                        .addGap(18, 18, 18)
                        .addComponent(spinLatitude, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnOK)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtLonSec, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel9)
                        .addGap(18, 18, 18)
                        .addComponent(spinLongitude, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtLatDeg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtLonDeg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(txtLonMin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8)
                            .addComponent(txtLonSec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9)
                            .addComponent(spinLongitude, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(txtLatMin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtLatSec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5)
                        .addComponent(jLabel10)
                        .addComponent(spinLatitude, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOK)
                    .addComponent(btnCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * Action untuk btnCancel
     */
        @Action
        public void clickCancel()
            {dispose();}

    /**
     * action untuk btnOK
     */
        @Action
        public void clickOK()
        {
            Vector< String > errorMsg = validateInput();

            if (errorMsg != null && errorMsg.size()>0)
            {
                StringBuffer messageBuffer = new StringBuffer();
                Iterator messages = errorMsg.iterator();

                while (messages.hasNext()) {
                    messageBuffer.append((String) messages.next());
                    messageBuffer.append("      " + System.getProperty("line.separator"));
                }

                JOptionPane.showMessageDialog(this, messageBuffer.toString(),
                        "Input Data Error", JOptionPane.ERROR_MESSAGE);

                return;
            }

            savePropertyAsDefault();
            
            CNSATMSMARTView view = (CNSATMSMARTView)CNSATMSMARTApp.getApplication().getMainView();
            view.updateRadarCenter();
            dispose();
        }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOK;
    private javax.swing.JComboBox spinLatitude;
    private javax.swing.JComboBox spinLongitude;
    private javax.swing.JTextField txtLatDeg;
    private javax.swing.JTextField txtLatMin;
    private javax.swing.JTextField txtLatSec;
    private javax.swing.JTextField txtLonDeg;
    private javax.swing.JTextField txtLonMin;
    private javax.swing.JTextField txtLonSec;
    // End of variables declaration//GEN-END:variables
}
